﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreeOnlineApplication.Class.Entity;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Collections;

namespace FreeOnlineApplication.Class.Repository
{
    public class LaundryRepository
    {
        protected MySqlConnection m_connection;
        MySqlCommand aCommand = new MySqlCommand();

        public LaundryRepository(MySqlConnection connection)
        {
            m_connection = connection;
            aCommand.Connection = m_connection;
        }
        public void Insert(Laundry laundry)
        {
            string insertSQL = String.Format(@"insert into table_laundry 
                (
                    laundry_code,
                    laundry_name,
                    laundry_serviceprice,
                    unit_id) 
                VALUES ('{0}','{1}',{2},{3})",
                laundry.CODE, laundry.NAME, laundry.SERVICE_PRICE, laundry.UNIT.ID);
            aCommand.CommandText = insertSQL;
            aCommand.ExecuteNonQuery();
            laundry.ID = GetMaximumId();
        }
        public Laundry GetById(int id)
        {
            string getByID = String.Format("select * from table_laundry where laundry_id = {0}", id);
            aCommand.CommandText = getByID;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            Laundry laundry = transform(aReader);
            aReader.Close();
            return laundry;
        }
        public void Update(Laundry laundry)
        {
            string updateSQL = String.Format(@"update table_laundry set 
                    laundry_code = '{0}',
                    laundry_name = '{1}',
                    laundry_serviceprice = {2},
                    unit_id = {3}
                where laundry_id = {4}",
                laundry.CODE, laundry.NAME, laundry.SERVICE_PRICE, laundry.UNIT.ID, laundry.ID);
            aCommand.CommandText = updateSQL;
            aCommand.ExecuteNonQuery();
        }
        public void Delete(Laundry laundry)
        {
            string deleteSQL = "delete from table_laundry where laundry_id = " + laundry.ID;
            aCommand.CommandText = deleteSQL;
            aCommand.ExecuteNonQuery();
        }
        public int GetMaximumId()
        {
            string getByID = String.Format("SELECT max(laundry_id) from table_laundry");
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public int GetCount(int unit)
        {
            string getByID = String.Format("SELECT count(*) from table_laundry where unit_id = {0}", unit);
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public IList GetAll(int laundry)
        {
            string sql = String.Format("select * from table_laundry where unit_id = {0}", laundry);
            aCommand.CommandText = sql;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            IList users = transformList(aReader);
            aReader.Close();
            return users;
        }
        private Laundry transform(MySqlDataReader aReader)
        {
            Laundry laundry = null;
            while (aReader.Read())
            {
                laundry = new Laundry();
                laundry.ID = Convert.ToInt32(aReader["laundry_id"]);
                laundry.CODE = aReader["laundry_code"].ToString();
                laundry.NAME = aReader["laundry_name"].ToString();
                laundry.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
                laundry.SERVICE_PRICE = Convert.ToDouble(aReader["laundry_serviceprice"]);
            }
            return laundry;
        }
        private IList transformList(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                Laundry laundry = new Laundry();
                laundry.ID = Convert.ToInt32(aReader["laundry_id"]);
                laundry.CODE = aReader["laundry_code"].ToString();
                laundry.NAME = aReader["laundry_name"].ToString();
                laundry.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
                laundry.SERVICE_PRICE = Convert.ToDouble(aReader["laundry_serviceprice"]);
                result.Add(laundry);
            }
            return result;
        }
    }
}
